#include "othertablemanagement.h"
AppointmentManagement::AppointmentManagement(SQLManagement *sqlm)
    : sql_m(sqlm)
{

}

bool AppointmentManagement::Insert(const QString &doctorId, const QString &patientId)
{
    // 构造 SQL 插入语句
    QString query = QString("INSERT INTO appointment_tb (patient_id, doctor_id) VALUES (%1, %2)")
                        .arg(patientId)
                        .arg(doctorId);

    if (sql_m)
    {
        return sql_m->executeQuery(query);
    }
    return false; // 如果 sql_m 为空，返回 false
}

QVariantList AppointmentManagement::GetPaitentInfoByDID(const QString &doctorId)
{
    QVariantList patientIds; // 用于存储患者 ID 列表

    // 构造 SQL 查询语句
    QString query = QString("SELECT patient_id FROM appointment_tb WHERE doctor_id = %1").arg(doctorId);

    // LOG_DEBUG() << "query: " << query;

    QVariantList queryResult = sql_m->executeQueryResultList(query, QueryType::PatientID);
    for (const auto &record : queryResult)
    {
        patientIds.append(record.toMap().value("patient_id"));
    }

    return patientIds; // 返回患者 ID 列表
}


AdviceManagement::AdviceManagement(SQLManagement *sqlm)
    : sql_m(sqlm)
{

}

bool AdviceManagement::Insert(const QString &doctorId, const QString &patientId, const QString &advice)
{
    QString query = QString("INSERT INTO advice_tb (patient_id, doctor_id, instruction, advice_date) VALUES ('%1', '%2', '%3', NOW())")
                    .arg(patientId, doctorId, advice);

    if (sql_m) {
        return sql_m->executeQuery(query);
    }
    LOG_ERROR() << "sql_m isn't exist";
    return false;
}

// 根据患者id，返回其对应的医嘱信息(instruction)与医生id(doctor_id)
QVariantMap AdviceManagement::GetInstructionByPID(const QString &patientId)
{
    QVariantMap instructionInfo; // 用于存储医嘱信息和医生 ID

    // 构造 SQL 查询语句
    QString query = QString("SELECT id, instruction, doctor_id FROM advice_tb WHERE patient_id = '%1'").arg(patientId);

    // LOG_DEBUG() << "query: " << query;

    QVariantList queryResult = sql_m->executeQueryResultList(query, QueryType::InstructionDetails);
    if (!queryResult.isEmpty()) {
        // 假设只获取第一个结果
        QVariantMap record = queryResult.first().toMap();
        instructionInfo["id"] = record.value("id");
        instructionInfo["instruction"] = record.value("instruction");
        instructionInfo["doctor_id"] = record.value("doctor_id");
    } else {
        LOG_ERROR() << "No instruction found for patient_id: " << patientId;
        return QVariantMap();
    }

    return instructionInfo; // 返回医嘱信息和医生 ID
}

QVariantList AdviceManagement::GetAdviceIdByID(const QString &patientId, const QString &doctorId)
{
    QVariantList adviceIds; // 用于存储医嘱 ID 的列表

    // 构造 SQL 查询语句
    QString query = QString("SELECT id FROM advice_tb WHERE patient_id = '%1' AND doctor_id = '%2'").arg(patientId, doctorId);

    // LOG_DEBUG() << "query: " << query;

    QVariantList queryResult = sql_m->executeQueryResultList(query, QueryType::AdviceIdDetails);
    if (!queryResult.isEmpty()) {
        foreach (const QVariant &item, queryResult) {
            QVariantMap record = item.toMap();
            adviceIds.append(record.value("id"));
        }
    } else {
        LOG_ERROR() << "No advice found for patient_id: " << patientId << " and doctor_id: " << doctorId;
    }

    return adviceIds; // 返回医嘱 ID 列表
}

QString AdviceManagement::GetDoctorIdByAdviceID(const QString &adviceId)
{
    QString doctorId; // 用于存储医生 ID

    // 构造 SQL 查询语句
    QString query = QString("SELECT doctor_id FROM advice_tb WHERE id = '%1'").arg(adviceId);

    LOG_DEBUG() << "query: " << query;

    QVariantList queryResult = sql_m->executeQueryResultList(query, QueryType::AdviceIdDetails);
    if (!queryResult.isEmpty()) {
        // 只关心第一条记录
        QVariantMap record = queryResult.first().toMap();
        doctorId = record.value("doctor_id").toString();
    } else {
        LOG_ERROR() << "No doctor found for advice_id: " << adviceId;
    }


    return doctorId; // 返回医生 ID
}

PrescriptionManagement::PrescriptionManagement(SQLManagement *sqlm)
    : sql_m(sqlm)
{

}

QVariantMap PrescriptionManagement::GetPrescriptionByPID(const QString &patientId)
{
    QVariantMap prescriptionInfo; // 用于存储处方信息

    // 构造 SQL 查询语句
    QString query = QString("SELECT advice_id, medication_name, dosage, frequency, cost FROM prescription_tb WHERE advice_id IN "
                            "(SELECT advice_id FROM advice_tb WHERE patient_id = '%1')").arg(patientId);

    // LOG_DEBUG() << "query: " << query;

    QVariantList queryResult = sql_m->executeQueryResultList(query, QueryType::PrescriptionDetails);
    if (!queryResult.isEmpty()) {
        // 假设只获取第一个结果
        QVariantMap record = queryResult.first().toMap();
        prescriptionInfo["advice_id"] = record.value("advice_id");
        prescriptionInfo["medication_name"] = record.value("medication_name");
        prescriptionInfo["dosage"] = record.value("dosage");
        prescriptionInfo["frequency"] = record.value("frequency");
        prescriptionInfo["cost"] = record.value("cost");
    } else {
        LOG_ERROR() << "No prescription found for patient_id: " << patientId;
        return QVariantMap();
    }

    LOG_DEBUG() << "prescriptionInfo: " << prescriptionInfo;
    return prescriptionInfo; // 返回处方信息
}

bool PrescriptionManagement::Insert(const QString &adviceId, const QString &medication, const QString &dosage, const QString &frequency, const QString &cost)
{
    // 构造 SQL 插入查询语句
    QString query = QString("INSERT INTO prescription_tb (advice_id, medication_name, dosage, frequency, cost) "
                            "VALUES ((SELECT advice_id FROM advice_tb WHERE patient_id = '%1' AND doctor_id = '%2' ORDER BY advice_date DESC LIMIT 1), '%3', '%4', '%5', '%6')")
                    .arg(adviceId, medication, dosage, frequency, cost); // 确保使用传入的参数

    if (sql_m) {
        if (sql_m->executeQuery(query)) {
            return true; // 插入成功
        } else {
            LOG_ERROR() << "Failed to execute query:" << query; // 记录执行失败的查询
        }
    } else {
        LOG_ERROR() << "sql_m doesn't exist";
    }
    return false; // 默认返回 false
}


MedicalRecordManagement::MedicalRecordManagement(SQLManagement *sqlm)
    : sql_m(sqlm)
{

}

bool MedicalRecordManagement::Insert(const QString &patientId, const QString &doctorId, const QString& diseaseName, const QString &details)
{
    QString query = QString("INSERT INTO medical_records_tb (patient_id, doctor_id, record_date, disease_name, details) VALUES ('%1', '%2', NOW(), '%3', '%4')")
                    .arg(patientId, doctorId, diseaseName, details);

    if (sql_m) {
        return sql_m->executeQuery(query);
    }
    LOG_ERROR() << "sql_m isn't exist";
    return false;
}

QVariantMap MedicalRecordManagement::GetMedicalRecordByPID(const QString &patientId)
{
    QVariantMap medicalRecordInfo; // 用于存储病历信息和医生 ID

    // 构造 SQL 查询语句
    QString query = QString("SELECT doctor_id, record_date, disease_name, details FROM medical_records_tb WHERE patient_id = '%1'").arg(patientId);

    // LOG_DEBUG() << "query: " << query;

    QVariantList queryResult = sql_m->executeQueryResultList(query, QueryType::MedicalRecordDetails);
    if (!queryResult.isEmpty()) {
        QVariantMap record = queryResult.first().toMap();
        medicalRecordInfo["disease_name"] = record.value("disease_name");
        medicalRecordInfo["doctor_id"] = record.value("doctor_id");
        medicalRecordInfo["record_date"] = record.value("record_date");
        medicalRecordInfo["details"] = record.value("details");
    } else {
        LOG_ERROR() << "No medical record found for patient_id: " << patientId;
        return QVariantMap();
    }

    // LOG_DEBUG() << "result: " << medicalRecordInfo;

    return medicalRecordInfo; // 返回病历信息和医生 ID
}
